﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ToolsLayer;
using BusinessEntity;
using Webcar.Interface;
namespace Webcar.BLL
{
    public class CarBLL
    {
       
        public CarBLL()
        {
           
        }

        private static readonly Webcar.Interface.ICar ic = Webcar.BLL.DataAccess.CreateCar();
        public static int GetPageNums()
        {
            return int.Parse(ResourceManager.GetString("CarListPageNum"));
        }

        public DataSet GetInsureSqlByCar(string carno)
        {
            return ic.GetInsureSqlByCar(carno);
        }

        public BS_CAR_SENDEntity GetCarSendInfo(string sendno)
        {
            BS_CAR_SENDEntity sendinfo = BS_CAR_SENDEntityAction.RetrieveABS_CAR_SENDEntity(sendno);
            if (sendinfo == null)
                throw new WBException(WBErrorType.NotExistsError);
            return sendinfo;
        }

        public BS_RUN_RECORDEntity GetCarRunInfo(int runno)
        {
            BS_RUN_RECORDEntity runinfo = new BS_RUN_RECORDEntity();
            runinfo.id = runno;
            runinfo.Retrieve();
            if (runinfo == null || !runinfo.IsPersistent)
                throw new WBException(WBErrorType.NotExistsError);
            return runinfo;
        }

        public BS_CAR_RECORDEntity GetCar(string carno)
        {
            if (string.IsNullOrEmpty(carno))
                throw new WBException(WBErrorType.PageArgumentError);

            BS_CAR_RECORDEntity ent = BS_CAR_RECORDEntityAction.RetrieveABS_CAR_RECORDEntity(carno);
            if(ent==null)
                throw new WBException(WBErrorType.NotExistsError);
            return ent;
        }
        public string GetCarNoByRecord(string recordno)
        {
            if (string.IsNullOrEmpty(recordno))
                return null;
            string sql = "select car_no from bs_car_record where record_no='"+recordno+"'";
            return DB.ExecuteScalar(sql);
        }

        public void DeleteCar(string carno)
        {
            BS_CAR_RECORDEntity Car = this.GetCar(carno);
            if (Car.IS_USE != "T")
            {
                Car.IS_USE = "T";
            }
            else
                Car.IS_USE = "D";
            Car.Save();
        }

        public void UpdateCar(BS_CAR_RECORDEntity obj)
        {
            this.CheckEntity(obj);
            
            //判断牌照号是否存在
            //如果是修改车辆，需要判断牌照号是否和现在的车辆相等。
            if (this.RecordIsExists(obj.RECORD_NO))
            {
                if (obj.IsPersistent)
                {
                    if(obj.RECORD_NO!=this.GetCar(obj.CAR_NO).RECORD_NO)
                        throw new WBException("牌照号" + obj.RECORD_NO + "已经存在。");
                }
                else
                    throw new WBException("牌照号" + obj.RECORD_NO + "已经存在。");
            }

            if (!obj.IsPersistent&&string.IsNullOrEmpty(obj.CAR_NO))
            {
                obj.CAR_NO = Webcar.BLL.SerialNoManager.GetCarNo();
                obj.IS_USE = "T";//设置默认值
            }
            obj.Save();

            InfoChecker ic = new InfoChecker();
            ic.SetInfoReaded(obj.CAR_NO, InfoCheckType.Car);

        }

        public string GetCarSort(string carno)
        {
            BS_CAR_CLASSEntity e = BS_CAR_CLASSEntityAction.RetrieveABS_CAR_CLASSEntity(this.GetCar(carno).CAR_CLASS);
            if (e==null)
                throw new WBException(WBErrorType.NotExistsError);
            return e.CAR_SORT;
        }
        /// <summary>
        /// 是否某牌照号已经存在。
        /// </summary>
        /// <param name="recordno"></param>
        /// <returns></returns>
        public bool RecordIsExists(string recordno)
        {
            string sql = "select count(1) from bs_car_record where record_no='" + recordno + "'";
            return ToolsLayer.DB.ExecuteScalar(sql) != "0";
        }

        public void CheckEntity(BS_CAR_RECORDEntity obj)
        {
            if (string.IsNullOrEmpty(obj.RECORD_NO))
                throw new WBException("请输入牌照号.");

            if (string.IsNullOrEmpty(obj.DEPT_CODE))
                throw new WBException( "请输入车辆所属单位.");

            if (string.IsNullOrEmpty(obj.CAR_USE))
                throw new WBException( "请输入车辆用途.");

            if (string.IsNullOrEmpty(obj.CAR_CLASS))
                throw new WBException( "请输入车型.");

            if (string.IsNullOrEmpty(obj.CAR_CLASS))
                throw new WBException( "请输入行驶证单位.");

            if (string.IsNullOrEmpty(obj.ASSET_DEPT))
                throw new WBException("请输入资产归属.");
        }

        /// <summary>
        /// 调动车辆
        /// </summary>
        public void MoveCar(string car_no,BS_CAR_MOVEEntity move_obj,String newChildDept)
        {
            //检查move_obj
            if (move_obj.MOVE_DATE ==DateTime.MinValue)
                throw new WBException("调动时间不能为空。");
            if (string.IsNullOrEmpty(move_obj.NEW_DEPT))
                throw new WBException("调动单位不能为空。");
            if (string.IsNullOrEmpty(move_obj.OPERATOR))
                throw new WBException("调动人不能为空。");
            
            BS_CAR_RECORDEntity car = this.GetCar(car_no);
            PersistenceLayer.Transaction t = new PersistenceLayer.Transaction();
            
            if (string.IsNullOrEmpty(move_obj.MOVE_NO))
                move_obj.MOVE_NO = Webcar.BLL.SerialNoManager.GetCarMoveNo();
            move_obj.OPER_DATE = DateTime.Now;
            move_obj.OLD_DEPT = car.DEPT_CODE;
            move_obj.CAR_NO = car_no;
            car.DEPT_CODE = move_obj.NEW_DEPT;
            car.CHILD_DEPT = newChildDept;
            if (move_obj.NEW_DEPT == move_obj.OLD_DEPT)
                throw new WBException("调动单位不能和原单位相同。");
            //保存
            t.AddSaveObject(car);
            t.AddSaveObject(move_obj);
            if (t.Process())
                t.Commit();
            else
                t.RollBack();
        }

        public void DiscardCar(string carno, BS_CAR_DISCARDEntity obj)
        {
            //检查move_obj
            if (obj.DISCARD_DATE ==DateTime.MinValue)
                throw new WBException("调动时间不能为空。");
            BS_CAR_RECORDEntity car = this.GetCar(carno);
            PersistenceLayer.Transaction t = new PersistenceLayer.Transaction();
            obj.CAR_NO = carno;
            obj.DEPT_CODE = car.DEPT_CODE;
            car.IS_USE = "F";
            String sql = "delete from bs_car_discard where car_no='" + carno + "'";
            t.AddSqlString(sql, "webcar");
            t.AddSaveObject(car);
            t.AddSaveObject(obj);
            if (t.Process())
                t.Commit();
            else
                t.RollBack();
        }
    }
}
